﻿using SuperX.Common.Object;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace SuperX.DataService.Abstract
{
    public class TreeNodeViewModel
    {
        public bool AlreadyExpanded { get; set; } = false;

        #region 节点属性

        /// <summary>
        /// 节点名称
        /// </summary>
        public string Name
        {
            get => _nodeName;
            set
            {
                if (_nodeName != value)
                {
                    _nodeName = value;
                }
            }
        }

        private string _nodeName;

        public string FullPath { get; set; }

        /// <summary>
        /// 本节点是否展开
        /// </summary>
        public bool IsExpanded
        {
            get => _isExpanded;
            set
            {
                if (_isExpanded != value)
                {
                    _isExpanded = value;
                }
            }
        }

        private bool _isExpanded = false;

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelected
        {
            get => _isSelected;
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;
                }
            }
        }

        private bool _isSelected = false;

        /// <summary>
        /// 父节点，根节点的父节点为null
        /// </summary>
        public TreeNodeViewModel ParentNode
        {
            get => _parentNode;
            set
            {
                if (_parentNode != value)
                {
                    _parentNode = value;
                }
            }
        }

        private TreeNodeViewModel _parentNode;

        /// <summary>
        /// 节点下的子节点集合
        /// </summary>
        public ObservableCollection<TreeNodeViewModel> ChildViewModels
        {
            get => _childNodes ?? (_childNodes = new ObservableCollection<TreeNodeViewModel>());
            set
            {
                if (_childNodes != value)
                {
                    _childNodes = value;
                }
            }
        }

        private ObservableCollection<TreeNodeViewModel> _childNodes;

        #endregion 节点属性

        public TreeNodeViewModel()
        {
            ChildViewModels = new ObservableCollection<TreeNodeViewModel>();
        }

        #region 对节点进行操作的方法

        /// <summary>
        /// 添加子节点，必须经此方法添加
        /// </summary>
        /// <param name="treeNodeModel"></param>
        public virtual void AddChildNode(object treeNodeModel)
        {
            try
            {
                if (treeNodeModel == null)
                {
                    return;
                }
                TreeNodeViewModel treeNodeBaseViewModel = (TreeNodeViewModel)treeNodeModel;
                treeNodeBaseViewModel.ParentNode = this;
                this.ChildViewModels.Add(treeNodeBaseViewModel);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取当前节点到根路径的全路径名
        /// </summary>
        /// <returns></returns>
        public virtual string GetFullPathNameToRoot()
        {
            string fullPathNameToRoot = "";
            if (ParentNode == null)
            {
                //代表为根节点
                fullPathNameToRoot = Name;
            }
            else
            {
                fullPathNameToRoot = ParentNode.GetFullPathNameToRoot() + "." + Name;
            }
            return fullPathNameToRoot;
        }

        #endregion 对节点进行操作的方法
    }
}